<template>
  <div class="card p10 col">
    <label class="title">取货机械手控制</label>
    <div class="row_center mt">
      <label>位置:</label>
      <el-input style="width: 60px;" v-model="ctx.position"/>
      <el-button @click="onClickCtrl" class="ml" type="primary">控制</el-button>
    </div>
    <div class="row_center mt">
      <el-select v-model="ctx.adj" style="width: 150px;">
        <el-option :label="ZERO" :value="ZERO"/>
        <el-option :label="INIT" :value="INIT"/>
        <el-option :label="PICK_LEFT" :value="PICK_LEFT"/>
        <el-option :label="PICK_RIGHT" :value="PICK_RIGHT"/>
        <el-option :label="HEAT" :value="HEAT"/>
        <el-option :label="RECYCLE" :value="RECYCLE"/>
      </el-select>
      <el-button @click="onClickAdj" class="ml" type="warning">校准</el-button>
      <el-button @click="onClickAdjQuery" class="ml" type="primary">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { dialog } from '../../service/ui';
import { fetch_adj, fetch_adj_query, fetch_ctrl } from '../../service/main_cmd';

const ZERO = "零点";
const INIT = "初始化位置";
const PICK_LEFT = "取货转进去位置";
const PICK_RIGHT = "取货转出来位置";
const HEAT = "保温门位置";
const RECYCLE = "出厂设置";

interface Ctx {
  position: string,
  adj: string,
}

const ctx = reactive<Ctx>({
  position: '512',
  adj: ZERO,
});

async function onClickCtrl() {
  try {
    const val = parseInt(ctx.position);
    await fetch_ctrl(val);
    dialog.succ('成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickAdj() {
  try {
    let type = 1;
    if (ctx.adj == INIT) {
      type = 2;
    } else if (ctx.adj == PICK_LEFT) {
      type = 3;
    } else if (ctx.adj == PICK_RIGHT) {
      type = 4;
    } else if (ctx.adj == HEAT) {
      type = 5;
    } else if (ctx.adj == RECYCLE) {
      type = 6;
    } 
    await fetch_adj(type);
    dialog.succ('校准成功');
  } catch (msg) {
    dialog.err(msg);
  }
}

async function onClickAdjQuery() {
  try {
    const info = await fetch_adj_query();
    dialog.succ(`零点:${info.zero}
      初始:${info.init}
      转进去:${info.left}
      转出来:${info.right}
      保温门:${info.heat}`, 0);
  } catch (msg) {
    dialog.err(msg);
  }
}

</script>

